﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Threading;

namespace ScanSite01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 定义判断80端口是否开放的函数
        private bool isOpenWeb(IPAddress tIP)
        {
            bool isOpen = false;
            try
            {
                TcpClient tcp = new TcpClient();
                //tcp.SendTimeout = 3;
                //tcp.ReceiveTimeout = 3;
                tcp.Connect(tIP, 80);
                tcp.Close();
                isOpen = true;
            }
            catch (Exception e)
            {
                //处理异常
                isOpen = false;
            }
            return isOpen;
        }

        /// <summary>
        /// 获取bingAPI数据
        /// </summary>
        /// <param name="sender">url</param>
        /// <param name="e"></param>
        private MatchCollection getBingData(string strUrl)
        {
            MatchCollection matches;
            WebClient client = new WebClient();
            string strReply = client.DownloadString(strUrl);
            Regex regTxt = new Regex("<a href=(.*?)><p>(.*?)</p>");
            matches = regTxt.Matches(strReply);
            return matches;
        }

        /// <summary>
        /// 根据IP生成文件目录
        /// </summary>
        /// <param name="strIP"></param>
        /// <returns></returns>
        private string fileName(string strIP)
        {
            string path = Environment.CurrentDirectory;
            path += "\\" + strIP.Split('.')[0] + "\\" + strIP.Split('.')[1] + "\\" + strIP.Split('.')[2];
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\" + strIP.Split('.')[3] + ".txt";
            return path;
        }

        /// <summary>
        /// 根据IP特点保存旁站数据到文件
        /// </summary>
        /// <param name="matches"></param>
        /// <param name="strIP"></param>
        private void writeToFile(MatchCollection matches, string strIP)
        {
            if (0 != matches.Count)
            {
                StreamWriter sw = new StreamWriter(fileName(strIP));
                if (checkBox2.Checked == false)
                {
                    foreach (Match m in matches)
                    {
                        sw.WriteLine(m.Groups[1].Value);
                    }
                }
                else
                {
                    foreach (Match m in matches)
                    {
                        sw.WriteLine(m.Groups[1].Value + "\t\t\t" + m.Groups[2].Value);
                    }
                }
                sw.Close();
            }
        }

        private void w(string IP)
        {

        }

        private void funcMan(string strBingUrl, string strIP)
        {
            try
            {
                if (checkBox1.Checked && isOpenWeb(IPAddress.Parse(strIP)))
                {
                    MatchCollection matches = getBingData(strBingUrl + "?ip=" + strIP);
                    writeToFile(matches, strIP);
                }
            }
            catch
            {
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //调试的代码
            //funcMan(textBox1.Text, "101.226.4.21");

            
            string IP = "";
            //按位获取IP地址，并保存为int结束
            if (radioButton1.Checked)
            {
                int startA = 0, startB = 0, startC = 0, startD = 0;
                int endA = 0, endB = 0, endC = 0, endD = 0;
                string startIP = textBox2.Text;
                string endIP = textBox3.Text;
                startA = Convert.ToInt16(startIP.Split('.')[0]);
                startB = Convert.ToInt16(startIP.Split('.')[1]);
                startC = Convert.ToInt16(startIP.Split('.')[2]);
                startD = Convert.ToInt16(startIP.Split('.')[3]);
                endA = Convert.ToInt16(endIP.Split('.')[0]);
                endB = Convert.ToInt16(endIP.Split('.')[1]);
                endC = Convert.ToInt16(endIP.Split('.')[2]);
                endD = Convert.ToInt16(endIP.Split('.')[3]);
                #region
                if (startA < endA)
                {
                    for (; startA <= endA; startA++)
                    {
                        if (startA == endA)
                        {
                            for (; startB <= endB; startB++)
                            {
                                if (startB == endB)
                                {
                                    for (; startC <= endC; startC++)
                                    {
                                        if (startC == endC)
                                        {
                                            for (; startD <= endD; startD++)
                                            {
                                                IP = startA.ToString() + "." + startB.ToString() + "." + startC.ToString() + "." + startD.ToString();
                                                funcMan(textBox1.Text, IP);
                                            }
                                        }
                                        else
                                        {
                                            for (int o = 0; o <= 255; o++)
                                            {
                                                IP = startA.ToString() + "." + startB.ToString() + "." + startC.ToString() + "." + o.ToString();
                                                funcMan(textBox1.Text, IP);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    for (int m = 1; m <= 255; m++)
                                    {
                                        for (int n = 1; n <= 255; n++)
                                        {
                                            IP = startA.ToString() + "." + startB.ToString() + "." + m.ToString() + "." + n.ToString();
                                            funcMan(textBox1.Text, IP);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (int i = 1; i <= 255; i++)
                            {
                                for (int j = 1; j <= 255; i++)
                                {
                                    for (int k = 1; k <= 255; i++)
                                    {
                                        IP = startA.ToString() + "." + i.ToString() + "." + j.ToString() + "." + k.ToString();
                                        funcMan(textBox1.Text, IP);
                                    }
                                }
                            }
                        }
                    }
                }
                else if ((startA == endA) && (startB < endB))
                {
                    for (; startB <= endB; startB++)
                    {
                        if (startB == endB)
                        {
                            for (; startC <= endC; startC++)
                            {
                                if (startC == endC)
                                {
                                    for (; startD <= endD; startD++)
                                    {
                                        IP = startA.ToString() + "." + startB.ToString() + "." + startC.ToString() + "." + startD.ToString();
                                        funcMan(textBox1.Text, IP);
                                    }
                                }
                                else
                                {
                                    for (int o = 0; o <= 255; o++)
                                    {
                                        IP = startA.ToString() + "." + startB.ToString() + "." + startC.ToString() + "." + o.ToString();
                                        funcMan(textBox1.Text, IP);
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (int m = 1; m <= 255; m++)
                            {
                                for (int n = 1; n <= 255; n++)
                                {
                                    IP = startA.ToString() + "." + startB.ToString() + "." + m.ToString() + "." + n.ToString();
                                    funcMan(textBox1.Text, IP);
                                }
                            }
                        }
                    }
                }
                else if ((startA == endA) && (startB == endB) && (startC < endC))
                {
                    for (; startC <= endC; startC++)
                    {
                        if (startC == endC)
                        {
                            for (; startD <= endD; startD++)
                            {
                                IP = startA.ToString() + "." + startB.ToString() + "." + startC.ToString() + "." + startD.ToString();
                                funcMan(textBox1.Text, IP);
                            }
                        }
                        else
                        {
                            for (int o = 0; o <= 255; o++)
                            {
                                IP = startA.ToString() + "." + startB.ToString() + "." + startC.ToString() + "." + o.ToString();
                                funcMan(textBox1.Text, IP);
                            }
                        }
                    }
                }
                else if ((startA == endA) && (startB == endB) && (startC == endC) && (startD < endD))
                {
                    for (; startD <= endD; startD++)
                    {
                        IP = startA.ToString() + "." + startB.ToString() + "." + startC.ToString() + "." + startD.ToString();
                        funcMan(textBox1.Text, IP);
                    }
                }
                else
                {
                    MessageBox.Show("Error", "can not deal with the IP");
                }
                //for (; startA <= endA; startA++)
                //{
                //    for (; startB <= endB; startB++)
                //    {
                //        for (; startC <= endC; startC++)
                //        {
                //            for (; startD <= endD; startD++)
                //            {
                //                string strIP = startA.ToString()+"."+startB.ToString()+"."+startC.ToString()+"."+startD.ToString();
                //                funcMan(textBox1.Text, strIP);
                //            }
                //        }
                //    }
                //}
                #endregion
            }
            else
            {
                StreamReader sr = new StreamReader(textBox4.Text);
                string sLine = "";
                while (sLine != null)
                {
                    sLine = sr.ReadLine();
                    if (sLine != null && sLine != "")
                    {
                        IP = Regex.Split(sLine, " 80 ", RegexOptions.IgnoreCase)[0].Trim();
                        if (IP != "")
                        {
                            funcMan(textBox1.Text, IP);
                        }
                    }
                }
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                label2.Enabled = true;
                label3.Enabled = true;
                label4.Enabled = false;
                textBox2.Enabled = true;
                textBox3.Enabled = true;
                textBox4.Enabled = false;
                button3.Enabled = false;
            }
            else
            {
                label2.Enabled = false;
                label3.Enabled = false;
                label4.Enabled = true;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
                textBox4.Enabled = true;
                button3.Enabled = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogFile = new OpenFileDialog();
            dialogFile.InitialDirectory = System.Environment.CurrentDirectory;
            if (dialogFile.ShowDialog() == DialogResult.OK)
            {
                string strResultFile = dialogFile.FileName;
                textBox4.Text = strResultFile;
            }
        }
    }
}
